<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        /* 
            1、返回一个函数
            2、传递参数

            举个栗子：
            var bar = {
                value: '1'
            }

            function foo() {
                console.log(this.value)
            }

            var foo2 = foo.bind(bar)
            foo2()          // 1
        */
        
        var bar = {
            value: '1'
        }

        function foo(name,age) {
            console.log(name)
            console.log(age)
            console.log(this.value)
        }

        Function.prototype.bind2 = function (context) {
            // 将this存储起来
            var self = this

            // arguments  获取传递的参数
            var args = Array.prototype.slice.call(arguments, 1)     // call 对象冒充

            return function() {
                var args2 = [].slice.call(arguments)
                self.apply(context, args.concat(args2))
            }
        }

        var foo2 = foo.bind2(bar, '张三')
        foo2('20')
    </script>
</body>
</html>